package com.demo2do.fms.entity.supplier.trans;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.format.PersistentFormat;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 * 运输的服务供方  特有字段
 * 
 * @author zhonghui
 *  
 */
@Entity
@Table(name = "FMS_TRANS_SUPPLIER")
@DynamicUpdate
public class TransSupplier {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
    @SequenceGenerator(name = "generator", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1)
    private Long id;

    /** 
     * 与supplier关联 
     * 
     * */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @PersistentFormat
    private Supplier supplier;

    /**
     * 运输工具保险下载
     */
    private String vehicleInsurance;

    /**
     * 单车月行驶公里数
     */
    private Long monthlyMileage;

    /**
     * 单车重载率
     */
    private BigDecimal heavyLoadRate;

    /**
     * GPS品牌
     * 01-亚加   02-安中  03-启明  04-赛格   05-长江通讯  06-东电投  07-智慧明   08-伊爱  00-其它
     * 枚举：GPSBrandType
     */
    private String gpsBrand;
    
    /**
     * 当GPS 是00-其它，将出现手填的GPS
     */
    private String otherGpsBrand;

    /**
     * GPS是否和安吉物流GPS平台兼容
     */
    @Column(name = "IS_COMPATIBLE")
    private boolean compatible;

    /**
     * GPS&TSS手持设备配备承诺书/购买合同上传
     */
    private String purchaseContract;

    /**
     * 商品车保险下载
     */
    private String goodsVehicleInsurance;

    /**
     * 是否注销
     */
    @Column(name = "IS_DISABLED")
    private Boolean disabled;

    /**
     * 创建时间
     */
    @Column(name = "CREATE_TIME")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     * 创建者
     */
    @Column(name = "CREATOR")
    private Long creator;

    /**
     * 最近修改时间
     */
    @Column(name = " LAST_MODIFY_TIME")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastModifyTime;

    /**
     * 最近修改者
     */
    @Column(name = "LAST_OPERATOR")
    private Long lastOperator;

    /**
     * The default constructor
     */
    public TransSupplier() {
    }

    /**
     * 
     * @param supplier
     */
    public TransSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    /**
     * @param operator
     * @return
     */
    public TransSupplier initialize(Long operator) {
        this.creator = operator;
        this.createTime = new Date();
        this.lastOperator = operator;
        this.lastModifyTime = new Date();
        this.disabled = false;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public TransSupplier edit() {
        this.lastModifyTime = new Date();
        this.lastOperator = null;
        return this;
    }
    
    /**
     * @param operator
     * @return
     */
    public TransSupplier disable(Long operator) {
        this.disabled = true;
        this.lastOperator = operator;
        this.lastModifyTime = new Date();
        return this;
    }
     
     /**
      * 
      * @return
      */
     public boolean isValid() {
         return StringUtils.isNoneBlank(this.vehicleInsurance) &&
                (this.monthlyMileage != null) &&
                (this.heavyLoadRate != null) &&
                (StringUtils.isNoneBlank(this.gpsBrand) || StringUtils.isNoneBlank(this.otherGpsBrand)) &&
                (StringUtils.isNoneBlank(this.purchaseContract)) &&
                (this.disabled == false);
     }
     
     /**
     * @return
     */
    public String inValidMessage() {
         if (StringUtils.isBlank(vehicleInsurance)) {
             return "运输工具保险未上传";
         } else if (monthlyMileage == null) {
             return "单车月行驶公里数不存在";
         } else if (heavyLoadRate == null) {
             return "单车重载率不存在";
         } else if (StringUtils.isBlank(gpsBrand)) {
             return "GPS品牌信息不全";
         } else if (StringUtils.isBlank(this.purchaseContract)) {
             return "GPS&TSS手持设备配备承诺书/购买合同未上传";
         } else if (disabled) {
             return "该运输供方已注销";
         } else {
             return null;
         }
     }

     /**
      * 
      * @return
      */
     public String getGpsDescription() {
         if(this.gpsBrand == null) {
             return null;
         } else {
             return GPSBrandType.codeOf(this.gpsBrand).getDescription();
         }
     }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the supplier
     */
    public Supplier getSupplier() {
        return supplier;
    }

    /**
     * @return the vehicleInsurance
     */
    public String getVehicleInsurance() {
        return vehicleInsurance;
    }

    /**
     * @return the monthlyMileage
     */
    public Long getMonthlyMileage() {
        return monthlyMileage;
    }

    /**
     * @return the heavyLoadRate
     */
    public BigDecimal getHeavyLoadRate() {
        return heavyLoadRate;
    }

    /**
     * @return the gpsBrand
     */
    public String getGpsBrand() {
        return gpsBrand;
    }

    /**
     * @return the otherGpsBrand
     */
    public String getOtherGpsBrand() {
        return otherGpsBrand;
    }

    /**
     * @return the compatible
     */
    public boolean isCompatible() {
        return compatible;
    }

    /**
     * @return the purchaseContract
     */
    public String getPurchaseContract() {
        return purchaseContract;
    }

    /**
     * @return the goodsVehicleInsurance
     */
    public String getGoodsVehicleInsurance() {
        return goodsVehicleInsurance;
    }

    /**
     * @return the disabled
     */
    public Boolean getDisabled() {
        return disabled;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the creator
     */
    public Long getCreator() {
        return creator;
    }

    /**
     * @return the lastModifyTime
     */
    public Date getLastModifyTime() {
        return lastModifyTime;
    }

    /**
     * @return the lastOperator
     */
    public Long getLastOperator() {
        return lastOperator;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param supplier the supplier to set
     */
    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    /**
     * @param vehicleInsurance the vehicleInsurance to set
     */
    public void setVehicleInsurance(String vehicleInsurance) {
        this.vehicleInsurance = vehicleInsurance;
    }

    /**
     * @param monthlyMileage the monthlyMileage to set
     */
    public void setMonthlyMileage(Long monthlyMileage) {
        this.monthlyMileage = monthlyMileage;
    }

    /**
     * @param heavyLoadRate the heavyLoadRate to set
     */
    public void setHeavyLoadRate(BigDecimal heavyLoadRate) {
        this.heavyLoadRate = heavyLoadRate;
    }

    /**
     * @param gpsBrand the gpsBrand to set
     */
    public void setGpsBrand(String gpsBrand) {
        this.gpsBrand = gpsBrand;
    }

    /**
     * @param otherGpsBrand the otherGpsBrand to set
     */
    public void setOtherGpsBrand(String otherGpsBrand) {
        this.otherGpsBrand = otherGpsBrand;
    }

    /**
     * @param compatible the compatible to set
     */
    public void setCompatible(boolean compatible) {
        this.compatible = compatible;
    }

    /**
     * @param purchaseContract the purchaseContract to set
     */
    public void setPurchaseContract(String purchaseContract) {
        this.purchaseContract = purchaseContract;
    }

    /**
     * @param goodsVehicleInsurance the goodsVehicleInsurance to set
     */
    public void setGoodsVehicleInsurance(String goodsVehicleInsurance) {
        this.goodsVehicleInsurance = goodsVehicleInsurance;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param creator the creator to set
     */
    public void setCreator(Long creator) {
        this.creator = creator;
    }

    /**
     * @param lastModifyTime the lastModifyTime to set
     */
    public void setLastModifyTime(Date lastModifyTime) {
        this.lastModifyTime = lastModifyTime;
    }

    /**
     * @param lastOperator the lastOperator to set
     */
    public void setLastOperator(Long lastOperator) {
        this.lastOperator = lastOperator;
    }
}
